<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 小虎哥 <1105415366@qq.com>
 * Date: 2018-4-3
 */

namespace think\template\taglib\api;

use think\Db;

/**
 * 文档收藏信息
 */
class TagCollect extends Base
{
    //初始化
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取文档收藏信息
     * @author wengxianhu by 2018-4-20
     */
    public function getCollect($aid = '', $type = 'default', $users = [])
    {
        if (empty($aid)) {
            return false;
        }
        
        $is_collect = 0;
        if (!empty($users['users_id'])) {
            $result = Db::name("users_collection")
                ->where(['aid' => $aid, 'users_id' => $users['users_id']])
                ->find();
            if (!empty($result)){
                $is_collect = 1;
            }
        }

        return [
            'is_collect'=>$is_collect
        ];
    }

    //获取收藏/喜欢(点赞)列表
    public function GetMyCollectList($users = [], $page = 1, $pagesize = 10)
    {

        $where['a.users_id'] = $users['users_id'];
        $where['a.aid'] = ['>',0];
        $where['c.status'] = 1;
        $where['c.is_del'] = 0;

        $paginate = ['page' => $page];
        $pages     = Db::name('users_collection')
            ->alias('a')
            ->field('d.typename,c.*,a.*,c.add_time as arc_add_time')
            ->where($where)
            ->join('archives c','a.aid = c.aid','left')
            ->join('arctype d', 'a.typeid = d.id','left')
            ->order('a.add_time desc')
            ->paginate($pagesize, false, $paginate);

        $result = $pages->toArray();
        if (!empty($result['data'])){
            $aidArr = get_arr_column($result['data'],'aid');
            $aid_count_arr = Db::name('users_collection')->where('aid','in',$aidArr)->group('aid')->field('count(id) as count,aid')->getAllWithIndex('aid');

            foreach ($result['data'] as $key => $val) {
                $val['is_litpic'] = 0;
                if (!empty($val['litpic'])){
                    $val['is_litpic'] = 1;
                }
                $val['litpic'] = $this->get_default_pic($val['litpic']); // 默认封面图
                $val['add_time'] = date('Y-m-d H:i:s',$val['add_time']);
                $val['arc_add_time_format'] = $this->time_format($val['arc_add_time']);
                $val['arc_add_time'] = date('Y-m-d H:i:s',$val['arc_add_time']);
                $val['update_time'] = date('Y-m-d H:i:s',$val['update_time']);
                $val['total_collect'] = !empty($aid_count_arr[$val['aid']]) ? $aid_count_arr[$val['aid']]['count'] : 0;
                $result['data'][$key] = $val;
            }
        }

        return $result;
    }
}